home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / CONTRIB / TEXT3D.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  13.9 KB  |  510 lines

  1. /* Text3d by Robert J. Doyle, Jr., Naval Research Laboratory, Washington, DC. */
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <math.h>
  6. #ifndef _WIN32
  7. #include <unistd.h>
  8. #endif
  9. #include <GL/glut.h>
  10.  
  11. typedef enum {RESERVED, M_SIDE, M_EDGE, M_WHOLE, O_SIDE, O_EDGE, O_WHOLE, 
  12. T_SIDE, T_EDGE, T_WHOLE, H_SIDE, H_EDGE, H_WHOLE,REPEAT_SIDE, REPEAT_EDGE, REPEAT1, 
  13. REPEAT2_SIDE, REPEAT2_EDGE,REPEAT2, REPEAT3_SIDE, REPEAT3_EDGE,REPEAT3, 
  14. REPEAT4_SIDE, REPEAT4_EDGE,REPEAT4} displayLists;
  15.  
  16. GLfloat sideColor[] = {0.0, 0.0, 0.5, 1.0};
  17. GLfloat edgeColor[] = {0.7, 0.7, 0.0, 1.0};
  18. GLfloat shininess[] = {128.0};
  19. GLfloat mat_specular[] = {0.7, 0.7, 0.7, 1.0};
  20.  
  21. GLfloat width = 0.0;
  22. GLfloat width2 = 2.0;
  23.  
  24. GLfloat letterM[][3] = 
  25.     {-3.125000, 0.000000, 0.000000},
  26.     {-3.125000, 6.208000, 0.000000},
  27.     {-1.233000, 6.208000, 0.000000},
  28.     {0.003000, 1.484000, 0.000000},
  29.     {1.223000, 6.208000, 0.000000},
  30.     {3.123000, 6.208000, 0.000000},
  31.     {3.123000, 0.000000, 0.000000},
  32.     {1.923000, 0.000000, 0.000000},
  33.     {1.923000, 5.010000, 0.000000},
  34.     {0.659000, 0.000000, 0.000000},
  35.     {-0.649000, 0.000000, 0.000000},
  36.     {-1.925000, 5.010000, 0.000000},
  37.     {-1.925000, 0.000000, 0.000000}
  38.  
  39. };
  40.  
  41. GLfloat letterO[][3] = 
  42.     {-3.038000, 3.102000, 0.000000},
  43.     {-2.974000, 3.874000, 0.000000},
  44.     {-2.827000, 4.440000, 0.000000},
  45.     {-2.802000, 4.508000, 0.000000},
  46.     {-2.544000, 5.042000, 0.000000},
  47.     {-2.502000, 5.110000, 0.000000},
  48.     {-2.223000, 5.479000, 0.000000},
  49.     {-2.132000, 5.576000, 0.000000},
  50.     {-1.784000, 5.869000, 0.000000},
  51.     {-1.678000, 5.940000, 0.000000},
  52.     {-1.260000, 6.155000, 0.000000},
  53.     {-1.148000, 6.198000, 0.000000},
  54.     {-0.677000, 6.321000, 0.000000},
  55.     {-0.638000, 6.328000, 0.000000},
  56.     {-0.002000, 6.378000, 0.000000},
  57.     {0.634000, 6.328000, 0.000000},
  58.     {1.107000, 6.210000, 0.000000},
  59.     {1.144000, 6.198000, 0.000000},
  60.     {1.570000, 6.002000, 0.000000},
  61.     {1.674000, 5.940000, 0.000000},
  62.     {2.038000, 5.661000, 0.000000},
  63.     {2.128000, 5.576000, 0.000000},
  64.     {2.428000, 5.217000, 0.000000},
  65.     {2.504000, 5.104000, 0.000000},
  66.     {2.762000, 4.598000, 0.000000},
  67.     {2.798000, 4.508000, 0.000000},
  68.     {2.960000, 3.913000, 0.000000},
  69.     {2.970000, 3.862000, 0.000000},
  70.     {3.034000, 3.102000, 0.000000},
  71.     {2.970000, 2.342000, 0.000000},
  72.     {2.815000, 1.745000, 0.000000},
  73.     {2.798000, 1.696000, 0.000000},
  74.     {2.554000, 1.182000, 0.000000},
  75.     {2.504000, 1.100000, 0.000000},
  76.     {2.221000, 0.726000, 0.000000},
  77.     {2.128000, 0.628000, 0.000000},
  78.     {1.776000, 0.332000, 0.000000},
  79.     {1.674000, 0.264000, 0.000000},
  80.     {1.256000, 0.049000, 0.000000},
  81.     {1.144000, 0.006000, 0.000000},
  82.     {0.672000, -0.117000, 0.000000},
  83.     {0.634000, -0.124000, 0.000000},
  84.     {-0.002000, -0.174000, 0.000000},
  85.     {-0.638000, -0.124000, 0.000000},
  86.     {-1.112000, -0.006000, 0.000000},
  87.     {-1.148000, 0.006000, 0.000000},
  88.     {-1.576000, 0.202000, 0.000000},
  89.     {-1.678000, 0.264000, 0.000000},
  90.     {-2.041000, 0.540000, 0.000000},
  91.     {-2.132000, 0.628000, 0.000000},
  92.     {-2.430000, 0.983000, 0.000000},
  93.     {-2.502000, 1.094000, 0.000000},
  94.     {-2.773000, 1.622000, 0.000000},
  95.     {-2.802000, 1.696000, 0.000000},
  96.     {-2.962000, 2.258000, 0.000000},
  97.     {-2.974000, 2.330000, 0.000000},
  98.     {-1.736000, 3.102000, 10000.0},
  99.     {-1.710000, 3.578000, 0.000000},
  100.     {-1.644000, 3.934000, 0.000000},
  101.     {-1.503000, 4.328000, 0.000000},
  102.     {-1.494000, 4.346000, 0.000000},
  103.     {-1.352000, 4.593000, 0.000000},
  104.     {-1.306000, 4.656000, 0.000000},
  105.     {-1.120000, 4.857000, 0.000000},
  106.     {-1.040000, 4.926000, 0.000000},
  107.     {-0.825000, 5.067000, 0.000000},
  108.     {-0.726000, 5.116000, 0.000000},
  109.     {-0.480000, 5.200000, 0.000000},
  110.     {-0.402000, 5.218000, 0.000000},
  111.     {-0.041000, 5.257000, 0.000000},
  112.     {-0.002000, 5.258000, 0.000000},
  113.     {0.361000, 5.227000, 0.000000},
  114.     {0.400000, 5.220000, 0.000000},
  115.     {0.650000, 5.147000, 0.000000},
  116.     {0.726000, 5.116000, 0.000000},
  117.     {0.950000, 4.990000, 0.000000},
  118.     {1.038000, 4.926000, 0.000000},
  119.     {1.239000, 4.736000, 0.000000},
  120.     {1.306000, 4.656000, 0.000000},
  121.     {1.462000, 4.413000, 0.000000},
  122.     {1.498000, 4.342000, 0.000000},
  123.     {1.635000, 3.964000, 0.000000},
  124.     {1.644000, 3.934000, 0.000000},
  125.     {1.710000, 3.568000, 0.000000},
  126.     {1.736000, 3.102000, 0.000000},
  127.     {1.710000, 2.636000, 0.000000},
  128.     {1.642000, 2.268000, 0.000000},
  129.     {1.508000, 1.886000, 0.000000},
  130.     {1.496000, 1.860000, 0.000000},
  131.     {1.351000, 1.610000, 0.000000},
  132.     {1.304000, 1.546000, 0.000000},
  133.     {1.115000, 1.343000, 0.000000},
  134.     {1.036000, 1.276000, 0.000000},
  135.     {0.823000, 1.135000, 0.000000},
  136.     {0.724000, 1.086000, 0.000000},
  137.     {0.480000, 1.001000, 0.000000},
  138.     {0.400000, 0.984000, 0.000000},
  139.     {0.035000, 0.946000, 0.000000},
  140.     {-0.002000, 0.946000, 0.000000},
  141.     {-0.368000, 0.979000, 0.000000},
  142.     {-0.402000, 0.986000, 0.000000},
  143.     {-0.653000, 1.057000, 0.000000},
  144.     {-0.726000, 1.088000, 0.000000},
  145.     {-0.952000, 1.213000, 0.000000},
  146.     {-1.040000, 1.278000, 0.000000},
  147.     {-1.240000, 1.467000, 0.000000},
  148.     {-1.306000, 1.548000, 0.000000},
  149.     {-1.460000, 1.788000, 0.000000},
  150.     {-1.494000, 1.858000, 0.000000},
  151.     {-1.639000, 2.251000, 0.000000},
  152.     {-1.644000, 2.270000, 0.000000},
  153.     {-1.710000, 2.626000, 0.000000}
  154. };
  155.  
  156. GLfloat letterT[][3] = 
  157. {
  158.     {-0.640000, 0.000000, 0.000000},
  159.     {-0.640000, 5.104000, 0.000000},
  160.     {-2.476000, 5.104000, 0.000000},
  161.     {-2.476000, 6.208000, 0.000000},
  162.     {2.476000, 6.208000, 0.000000},
  163.     {2.476000, 5.104000, 0.000000},
  164.     {0.640000, 5.104000, 0.000000},
  165.     {0.640000, 0.000000, 0.000000}
  166. };
  167.  
  168. GLfloat letterH[][3] = 
  169. {
  170.     {-2.570000, 0.000000, 0.000000},
  171.     {-2.570000, 6.208000, 0.000000},
  172.     {-1.282000, 6.208000, 0.000000},
  173.     {-1.282000, 3.900000, 0.000000},
  174.     {1.280000, 3.900000, 0.000000},
  175.     {1.280000, 6.208000, 0.000000},
  176.     {2.568000, 6.208000, 0.000000},
  177.     {2.568000, 0.000000, 0.000000},
  178.     {1.280000, 0.000000, 0.000000},
  179.     {1.280000, 2.760000, 0.000000},
  180.     {-1.282000, 2.760000, 0.000000},
  181.     {-1.282000, 0.000000, 0.000000}
  182. };
  183.  
  184. /*  Initialize light source and lighting.
  185.  */
  186.  
  187.  
  188. static void checkErrors(void)
  189. {
  190.   GLenum error;
  191.   while ((error = glGetError()) != GL_NO_ERROR) {
  192.     fprintf(stderr, "Error: %s\n", (char *) gluErrorString(error));
  193.   }
  194. }
  195.  
  196. void myinit(void)
  197. {
  198.     int count1 =  sizeof(letterM) / (3 * sizeof(GLfloat)); 
  199.     int count2 =  sizeof(letterO) / (3 * sizeof(GLfloat));
  200.     int count3 =  sizeof(letterT) / (3 * sizeof(GLfloat)); 
  201.     int count4 =  sizeof(letterH) / (3 * sizeof(GLfloat));
  202.  
  203.     int i;
  204.     
  205.     GLfloat light_ambient[] = { 1.0, 1.0, 1.0, 1.0 };
  206.     GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
  207.     GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  208. /*    light_position is NOT default value    */
  209.     GLfloat light_position[] = { -1.0, -1.0, 1.0, 0.0 };
  210.  
  211.     glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  212.     glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  213.     glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  214.     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  215.     
  216.     glEnable(GL_LIGHTING);
  217.     glEnable(GL_LIGHT0);
  218.     glDepthFunc(GL_LESS);
  219.     glEnable(GL_DEPTH_TEST);
  220.  
  221.     glDrawBuffer(GL_FRONT_AND_BACK);
  222.     glClearColor(0.0, 0.0, 0.0, 1.0);
  223.     glClear(GL_COLOR_BUFFER_BIT);
  224.     glClear(GL_ACCUM_BUFFER_BIT);
  225.     glDrawBuffer(GL_BACK);    
  226.  
  227. /* Zero position of text */
  228.  
  229.             for(i = 0; i < count1; i++) {
  230.                 letterM[i][1] = letterM[i][1] - 3.175;
  231.             }
  232.             for(i = 0; i < count2; i++) {
  233.                 letterO[i][1] = letterO[i][1] - 3.175;
  234.             }
  235.             for(i = 0; i < count3; i++) {
  236.                 letterT[i][1] = letterT[i][1] - 3.175;
  237.             } 
  238.             for(i = 0; i < count4; i++) {
  239.                 letterH[i][1] = letterH[i][1] - 3.175;
  240.             } 
  241. }
  242.  
  243.  
  244. /* Mark Kilgard's tessellation code from the "dino" demos. */
  245. void extrudeSolidFromPolygon(GLfloat data[][3], unsigned int dataSize,
  246.         GLdouble thickness, GLuint side, GLuint edge, GLuint whole)
  247. {
  248.     GLdouble vertex[3], dx, dy, len;
  249.     int i, k;
  250.     int flag = 0;
  251.     int count = dataSize / (3 * sizeof(GLfloat));
  252.     static GLUtriangulatorObj *tobj = NULL;
  253.  
  254.     if (tobj == NULL) {
  255.         tobj = gluNewTess();
  256.         
  257.         gluTessCallback(tobj, GLU_BEGIN, glBegin);
  258.         gluTessCallback(tobj, GLU_VERTEX, glVertex3fv);
  259.         gluTessCallback(tobj, GLU_END, glEnd);
  260.     }
  261.     glNewList(side, GL_COMPILE);
  262.         glShadeModel(GL_SMOOTH);
  263.         gluBeginPolygon(tobj);
  264.             for(i = 0; i < count; i++) {
  265.                 /* This detects a new contour from a large number placed in
  266.                 the unused z coordinate of the vertex where the new contour 
  267.                 starts. See the coordinates for letterO, above. The coordinate 
  268.                 must be reset below for additional calls. */
  269.  
  270.                 if (data[i][2] > 1000.0) {
  271.                     data[i][2] = 0.0;
  272.                     flag = 1; k = i;
  273.                     gluNextContour(tobj, GLU_INTERIOR); 
  274.                 }
  275.                 
  276.                 vertex[0] = data[i][0];
  277.                 vertex[1] = data[i][1];
  278.                 vertex[2] = 0.0;
  279.                 gluTessVertex(tobj, vertex, data[i]);
  280.             }
  281.         gluEndPolygon(tobj);
  282.     glEndList();
  283.     
  284.                 /* Reset coordinate for new calls. */
  285.                 if (flag == 1) {
  286.                 data[k][2] = 10000.0;
  287.                 flag = 0;
  288.                 }
  289.     glNewList(edge, GL_COMPILE);
  290.         glBegin(GL_QUAD_STRIP);
  291.         for(i = 0; i <= count; i++) {
  292.             glVertex3f(data[i % count][0], data[i % count][1], 0.0);
  293.             glVertex3f(data[i % count][0], data[i % count][1], thickness);
  294.             /* Normals */
  295.             dx = data[(i+ 1) % count][1] - data[i % count][1];
  296.             dy = data[i % count][0] - data[(i + 1) % count][0];
  297.             len = sqrt(dx * dx + dy * dy);
  298.             glNormal3f(dx / len, dy / len, 0.0);
  299.         }
  300.         glEnd();
  301.     glEndList();
  302.     
  303.     glNewList(whole, GL_COMPILE);
  304.         glFrontFace(GL_CW);
  305.  
  306.         glMaterialfv(GL_FRONT, GL_DIFFUSE, edgeColor);
  307.         glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
  308.         glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);                
  309.  
  310.         glCallList(edge);
  311.         glNormal3f(0.0, 0.0, -1.0); 
  312.         glCallList(side);
  313.         glPushMatrix();
  314.             glTranslatef(0.0, 0.0, thickness);
  315.             glFrontFace(GL_CCW);
  316.             glNormal3f(0.0, 0.0, 1.0);
  317.  
  318.         glMaterialfv(GL_FRONT, GL_DIFFUSE, sideColor);
  319.         glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
  320.         glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);        
  321.         
  322.             glCallList(side);
  323.         glPopMatrix();
  324.     glEndList();
  325. }
  326.  
  327. void repeat(int j)
  328. {
  329.     if(j == 2){            
  330.         glPushMatrix();
  331.             glTranslatef((31 * -0.34) , 9.3, -9.6);
  332.             glCallList(REPEAT1);
  333.         glPopMatrix();                
  334.     }
  335.     if(j == 3){            
  336.         glPushMatrix();
  337.             glTranslatef(31 * -0.34, 9.3, -9.6);
  338.             glCallList(REPEAT1);
  339.         glPopMatrix();
  340.         glPushMatrix();
  341.             glTranslatef(31 * -.09, 9.3, -9.6);
  342.             glCallList(REPEAT2);
  343.         glPopMatrix();    
  344.     }
  345.     if(j == 4){            
  346.         glPushMatrix();
  347.             glTranslatef(31 * -0.34, 9.3, -9.6);
  348.             glCallList(REPEAT1);
  349.         glPopMatrix();
  350.         glPushMatrix();
  351.             glTranslatef(31 * -.09, 9.3, -9.6);
  352.             glCallList(REPEAT2);
  353.         glPopMatrix();    
  354.         glPushMatrix();
  355.             glTranslatef(31 * 0.12, 9.3, -9.6);
  356.             glCallList(REPEAT3);
  357.         glPopMatrix();
  358.     }    
  359. }
  360.  
  361. void display(void)
  362. {
  363.     int i, j;
  364.     GLfloat xPos = -0.34;
  365.     glLoadIdentity();
  366.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  367.         
  368.     glTranslatef(0.0, 0.0, -10.0);
  369.  
  370.     extrudeSolidFromPolygon(letterM, sizeof(letterM), width2, REPEAT_SIDE, 
  371.         REPEAT_EDGE, REPEAT1);
  372.     extrudeSolidFromPolygon(letterO, sizeof(letterO), width2, REPEAT2_SIDE, 
  373.         REPEAT2_EDGE, REPEAT2);
  374.     extrudeSolidFromPolygon(letterT, sizeof(letterT), width2, REPEAT3_SIDE, 
  375.         REPEAT3_EDGE, REPEAT3);
  376.     extrudeSolidFromPolygon(letterH, sizeof(letterH), width2, REPEAT4_SIDE, 
  377.         REPEAT4_EDGE, REPEAT4);            
  378.  
  379.     for(j = 1; j < 5; j++){ 
  380.         width = 0.0;
  381. checkErrors();
  382.         for(i = 0; i < 10; i++){ 
  383.  
  384.             glPushMatrix();
  385.                 repeat(j);
  386.             glPopMatrix();
  387.             
  388.             glPushMatrix();
  389.                 glRotatef(90.0, 0.0, 1.0, 0.0); 
  390.                 if(j == 1){
  391.                     extrudeSolidFromPolygon(letterM, sizeof(letterM), width, M_SIDE, 
  392.                         M_EDGE, M_WHOLE);
  393.                     glCallList(M_WHOLE);                    
  394.                 }
  395.                 if(j == 2){
  396.                     extrudeSolidFromPolygon(letterO, sizeof(letterO), width, O_SIDE, 
  397.                         O_EDGE, O_WHOLE);
  398.                     glCallList(O_WHOLE);
  399.                 }                
  400.                 if(j == 3){
  401.                     extrudeSolidFromPolygon(letterT, sizeof(letterT), width, T_SIDE, 
  402.                         T_EDGE, T_WHOLE);
  403.                     glCallList(T_WHOLE);
  404.                 }
  405.                 if(j == 4){
  406.                     extrudeSolidFromPolygon(letterH, sizeof(letterH), width, H_SIDE, 
  407.                         H_EDGE, H_WHOLE);
  408.                     glCallList(H_WHOLE);
  409.                 }                
  410.                 glutSwapBuffers();
  411.                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  412.                 width = width + 0.2;
  413.             glPopMatrix();
  414.         }
  415.         for(i = 0; i < 45 ; i++){
  416.                 
  417.             glPushMatrix();
  418.                 repeat(j);
  419.             glPopMatrix();                    
  420.             
  421.             glPushMatrix();                    
  422.                 glRotatef(90.0 - (2.0 * i), 0.0, 1.0, 0.0);
  423.                 if(j == 1){
  424.                 glCallList(M_WHOLE);
  425.                 }
  426.                 if(j == 2){
  427.                 glCallList(O_WHOLE);
  428.                 }
  429.                 if(j == 3){
  430.                 glCallList(T_WHOLE);
  431.                 }
  432.                 if(j == 4){
  433.                 glCallList(H_WHOLE);
  434.                 }            
  435.                 glutSwapBuffers();
  436.                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);        
  437.             glPopMatrix();
  438.         } 
  439.         for(i = 1; i < 32 ; i++){
  440.             
  441.             glPushMatrix();
  442.                 repeat(j);
  443.             glPopMatrix();
  444.             
  445.             glPushMatrix();
  446.                 glTranslatef(i * xPos, i * 0.3, i * -0.3);
  447.                 if(j == 1){
  448.                 glCallList(M_WHOLE);
  449.                 }
  450.                 if(j == 2){
  451.                 glCallList(O_WHOLE);
  452.                 }
  453.                 if(j == 3){
  454.                 glCallList(T_WHOLE);
  455.                 }
  456.                 if(j == 4){
  457.                 glCallList(H_WHOLE);
  458.                 }        
  459.                 glutSwapBuffers();
  460.                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);                
  461.             glPopMatrix();
  462.         }                
  463.  
  464.         if(j == 1){
  465.             xPos = xPos + 0.25;
  466.         }
  467.         else{
  468.             xPos = xPos + 0.21;
  469.         }
  470.     }
  471.     glFlush();
  472. }
  473.  
  474. void myReshape(int w, int h)
  475. {
  476.     glViewport(0, 0, w, h);
  477.     glMatrixMode(GL_PROJECTION);
  478.     glLoadIdentity();
  479.   glFrustum(-7.0, 7.0, -7.0, 7.0, 6.0, 20.0);
  480. /*    if (w <= h) 
  481.     glOrtho (-7.0, 7.0, -7.0*(GLfloat)h/(GLfloat)w, 
  482.         7.0*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
  483.     else 
  484.     glOrtho (-7.0*(GLfloat)w/(GLfloat)h, 
  485.         7.0*(GLfloat)w/(GLfloat)h, -7.0, 7.0, -10.0, 10.0); */
  486.     glMatrixMode(GL_MODELVIEW);
  487. }
  488.  
  489. /*  Main Loop
  490.  *  Open window with initial window size, title bar, 
  491.  *  RGBA display mode, and handle input events.
  492.  */
  493. int main(int argc, char** argv)
  494. {
  495.     glutInit(&argc, argv);
  496.     glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  497.     glutCreateWindow ("text3d");
  498.  
  499. /*glCullFace(GL_FRONT);*/
  500. /*glEnable(GL_CULL_FACE);*/
  501.  
  502.     myinit();
  503.     glutReshapeFunc (myReshape);
  504.     glutDisplayFunc (display);
  505.     glutMainLoop();
  506.     return 0;             /* ANSI C requires main to return int. */
  507. }
  508.